<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
<head>
	<meta charset='utf-8' /> 
	<title>台账设置</title>
	<style>
		.cursers{cursor:pointer}
	</style>
</head>
<body style='background:#ddd;padding:0px;margin:0px;padding-left:10px'>
	<div style='border:0px solid;font:normal normal 12px/30px normal'>
		<div style='float:left;padding-left:0px'>　
			<span style='color:blue' class='cursers' onclick=_delLedger()>删除台账</span>
		</div>
		<div style='float:right;padding-right:20px'>　
			<span style='color:blue' class='cursers' onclick='history.go(-1)'>返回</span>
		</div>
		<div style='clear:both'></div>
	</div>

	<div>
		<h1 style='text-align:center'>台账设置--<span id='ledgerName'><<台账名称>></span></h1>		
	</div>

	<div style='text-align:center' id='fieldList'>
		<div style='margin:5px 0'>字段名：<span style='font-size:12px'>aaa</span>　　字段标识：<span style='font-size:12px'>bbb</span>　　<button>删除</button>　　<button>置为分类</button></div>
		<div style='margin:5px 0'>字段名：<span style='font-size:12px'>aaa</span>　　字段标识：<span style='font-size:12px'>bbb</span>　　<button>删除</button>　　<button>置为搜索</button></div>
		<div style='margin:5px 0'>字段名：<span style='font-size:12px'>aaa</span>　　字段标识：<span style='font-size:12px'>bbb</span>　　<button>删除</button>　　<button style='background:#aaa'>取消分类</button></div>
		<div style='margin:5px 0'>字段名：<span style='font-size:12px'>aaa</span>　　字段标识：<span style='font-size:12px'>bbb</span>　　<button>删除</button>　　<button>置为分类</button></div>
		<div style='margin:5px 0'>字段名：<span style='font-size:12px'>aaa</span>　　字段标识：<span style='font-size:12px'>bbb</span>　　<button>删除</button>　　<button style='background:#aaa'>取消搜索</button></div>
	</div>

	<div style='text-align:center;margin:20px;'>
		<div id='addArea' style='display:none'>
			<div>字段名称：<input type="text" placeholder='必填' id='fieldName' /></div>
			<div>字段标识：<input type="text" placeholder='字母' id='fieldTname' /></div>
			<div style='margin:10px'>
				<button id='addFieldSub'>提交</button>
				<button id='addBack'>返回</button>
			</div>
		</div>
		<button id='addBtn'>添加字段</button>
	</div>




</body>
</html>
<script src='../public/jquery1.1.js'></script>
<script src='../public/fun.js'></script>
<script>
if(!sessionStorage.ledgerCurrentTname){
	A('缺少表名！');
	history.go(-1);
}

var fieldsList;


$(function(){
	$('#ledgerName').text(sessionStorage.ledgerCurrentName);	//显示台账名

	_getOrCheckField(function(fieldsListRes){		//获取台账的字段列表
		fieldsList = fieldsListRes;					//保存字段列表到容器
		_parseData();
	});

	$('#addBtn').click(function(){
		$(this).hide();
		$('#addArea').show();;
	});

	$('#addBack').click(function(){
		$('#addBtn').show();
		$('#addArea').hide();
	});

	//添加新字段提交 
	$('#addFieldSub').click(function(){
		var fieldName = m_trim($('#fieldName').val());
			if(!fieldName || !_checkChar(fieldName)){
				A('非法的字段名称！');return;
			}
		var fieldTname = m_trim($('#fieldTname').val());
			if(!/^[a-zA-z]\w+$/.test(fieldTname)){
				A('字段只能以字母加数字组合！');return;
			}

		var itype = $('input[name=itype]:checked').val();
		var data = {
			type:'addField',
			tname:sessionStorage.ledgerCurrentTname,
			fieldName:fieldName,
			fieldTname:fieldTname
		}
		AjaxBefore(data,function(res){
			var tmp = _checkJsonFormat(res); //检测JSON格式
			if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
				A('添加成功！');
				$('#fieldName,#fieldTname').val('');
				location.reload();
			}
			if(tmp.stat == 'err3'){
				A('字段标识已存在!');
			}
			if(tmp.stat == 'err4'){
				A('非法字符!');
			}
		});
	});
});










//清除原有的字段列表缓存
function _clearFieldsListSess(){
	var sessName = 'sessionStorage.fieldsList_'+sessionStorage.ledgerCurrentTname+"=''";
	eval(sessName);
}





//删除台账
function _delLedger(){
	if(!confirm('确认删除该台账？')){
		return;
	}
	var vcode = Math.round(Math.random()*10000);
	if(vcode != prompt('请输入确认码：'+vcode)){
		A('确认码不正确！');
		return;
	}
	var data = {
		type:'_delLedger',
		tname:sessionStorage.ledgerCurrentTname
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
			A('删除成功！');
			location.replace('main.html');
		}else{
			A('删除失败！');
		}
	});
}



//解析字段数据
function _parseData(){
	var data = fieldsList;

	var tmp_str = '';
	for(var i=0;i<data.length;i++){
		if(data[i].isSelect){	//是分类字段
			var select_str = "<button style='background:#aaa' onclick=_setSelect('" + data[i].fieldTname + "',1)>取消分类</button>";
		}else{
			var select_str = "<button onclick=_setSelect('" + data[i].fieldTname + "')>置为分类</button>";
		}

		if(data[i].isSearch){
			var search_str = "<button style='background:#aaa' onclick=_setSearch('" + data[i].fieldTname + "')>取消搜索</button>";
		}else{
			var search_str = "<button onclick=_setSearch('" + data[i].fieldTname + "')>置为搜索</button>";
		}

		if(data[i].isArea){
			var area_str = "<button style='background:#aaa' onclick=_changeArea('" + data[i].fieldTname + "')>转输入框</button>";
		}else{
			var area_str = "<button onclick=_changeArea('" + data[i].fieldTname + "')>转文本域</button>";
		}

		tmp_str += "<div style='margin:5px 0'>字段名：<span style='font-size:12px'>" + data[i].fieldName + "</span>　　字段标识：<span style='font-size:12px'>" + data[i].fieldTname + "</span>　　<button onclick=_delField('" + data[i].fieldTname + "')>删除</button>　　"+select_str+"　　"+search_str+"　　"+area_str+"</div>";
	}
	$('#fieldList').html(tmp_str);
}




//转换文本域
function _changeArea(fname){
	if(!confirm('确认该操作？')){
		return;
	}
	var data = {
		type:'_changeArea',
		fname:fname,
		tname:sessionStorage.ledgerCurrentTname
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
			A('设置成功！');
			location.reload();
		}else{
			A('设置失败！');
		}
	});
}




//置为搜索项
function _setSearch(fname){
	if(!confirm('确认该操作？')){
		return;
	}
	var data = {
		type:'_setSearch',
		fname:fname,
		tname:sessionStorage.ledgerCurrentTname
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
			A('设置成功！');
			location.reload();
		}else{
			A('设置失败！');
		}
	});
}




//置为分类项
function _setSelect(fname,qtype){
	if(!confirm('确认该操作？')){
		return;
	}
	var data = {
		type:'_setSelect',
		fname:fname,
		tname:sessionStorage.ledgerCurrentTname,
		qtype:qtype
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
			A('设置成功！');
			location.reload();
		}else{
			A('设置失败！');
		}
	});
}




//删除字段
function _delField(fname){
	if(!confirm('确认该操作？')){
		return;
	}
	var vcode = Math.round(Math.random()*10000);
	if(vcode != prompt('请输入确认码：'+vcode)){
		A('确认码不正确！');
		return;
	}
	var data = {
		type:'_delField',
		fname:fname,
		tname:sessionStorage.ledgerCurrentTname
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			_clearFieldsListSess();		//清除原有的字段列表缓存
			A('删除成功！');
			location.reload();
		}else{
			A('删除失败！');
		}
	});
}



</script>

